package com.atguigu.tingshu.order.template.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.enums.user.IsVipEnum;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.user.UserPaidAlbum;
import com.atguigu.tingshu.order.template.OrderConfirmTemplate;
import com.atguigu.tingshu.order.template.TemplateBean;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.user.client.UserPaidAlbumFeignClient;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;

@TemplateBean(type = SystemConstant.ORDER_ITEM_TYPE_ALBUM)
public class AlbumOrderConfirm extends OrderConfirmTemplate {

    @Autowired
    private UserPaidAlbumFeignClient userPaidAlbumFeignClient;

    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Override
    public void trade(OrderInfoVo orderInfoVo, TradeVo tradeVo) {
        // 根据专辑id查询当前用户是否已购买该专辑
        Long userId = AuthContextHolder.getUserId();
        Result<UserPaidAlbum> userPaidAlbumResult = userPaidAlbumFeignClient.findUserPaidAlbum(userId, tradeVo.getItemId());
        org.springframework.util.Assert.notNull(userPaidAlbumResult, "查询用户购买的专辑信息异常");
        UserPaidAlbum userPaidAlbum = userPaidAlbumResult.getData();
        if (userPaidAlbum != null) throw new GuiguException(ResultCodeEnum.CANNOT_PAY_AGAIN);
        // 根据专辑id查询专辑信息
        Result<AlbumInfo> albumInfoResult = albumInfoFeignClient.getAlbumInfo(tradeVo.getItemId());
        org.springframework.util.Assert.notNull(albumInfoResult, "查询专辑信息异常");
        AlbumInfo albumInfo = albumInfoResult.getData();
        // 根据用户id查询用户信息
        Result<UserInfoVo> userInfoVoResult = userInfoFeignClient.getUserInfoVo(userId);
        Assert.notNull(userInfoVoResult, "查询用户信息异常");
        UserInfoVo userInfoVo = userInfoVoResult.getData();
        // 计算专辑价格
        BigDecimal originalAmount = albumInfo.getPrice();
        orderInfoVo.setOriginalAmount(originalAmount);
        BigDecimal orderAmount = originalAmount;
        if (userInfoVo.getIsVip().equals(IsVipEnum.IS_VIP.getCode()) && userInfoVo.getVipExpireTime().after(new Date())) {
            if (albumInfo.getVipDiscount().compareTo(new BigDecimal("-1")) != 0) {
                orderAmount = originalAmount.multiply(albumInfo.getVipDiscount().divide(new BigDecimal("10")));
            }
        } else {
            if (albumInfo.getDiscount().compareTo(new BigDecimal("-1")) != 0) {
                orderAmount = originalAmount.multiply(albumInfo.getDiscount().divide(new BigDecimal("10")));
            }
        }
        orderInfoVo.setOrderAmount(orderAmount);
        BigDecimal derateAmount = originalAmount.subtract(orderAmount);
        orderInfoVo.setDerateAmount(derateAmount);
        // 生成订单明细列表
        orderInfoVo.setOrderDetailVoList(
                Arrays.asList(OrderDetailVo.builder()
                        .itemId(tradeVo.getItemId())
                        .itemName(albumInfo.getAlbumTitle())
                        .itemPrice(orderAmount)
                        .itemUrl(albumInfo.getCoverUrl())
                        .build())
        );
        // 生成订单减免列表
        orderInfoVo.setOrderDerateVoList(
                Arrays.asList(OrderDerateVo.builder()
                        .derateType(SystemConstant.ORDER_DERATE_ALBUM_DISCOUNT)
                        .derateAmount(derateAmount)
                        .remarks("专辑折扣")
                        .build())
        );
    }
}
